/*This file is part of BinaryReaderCP.

 *  BinaryReaderCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  BinaryReaderCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with BinaryReaderCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#include "stdafx.h"
#include "BinaryReader.h"

#include <iostream>

#define	FS	(*fileStream)

using namespace std;

BinaryReader::BinaryReader(){
	this->filePointer=NULL;
}

BinaryReader::BinaryReader(FILE* fp){
	/*fileStream=stream;
	length=-1;//*/
	this->filePointer=fp;
	this->length=-1;
	if(this->filePointer!=NULL)
		this->pos=ftell(this->filePointer);
	else
		this->pos=-1;
}

BinaryReader::BinaryReader(const char* fPath){
	this->filePointer=fopen(fPath,"rb");
	this->length=-1;
	if(this->filePointer!=NULL)
		this->pos=ftell(this->filePointer);
	else
		this->pos=-1;
}

bool BinaryReader::Opened(){
	return !(this->filePointer==NULL);
}

void BinaryReader::Close(){
	fclose(this->filePointer);
}

signed char BinaryReader::ReadSByte(){
	signed char ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

short BinaryReader::ReadInt16(){
	short ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

int BinaryReader::ReadInt32(){
	int ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

float BinaryReader::ReadSingle(){
	float ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

double BinaryReader::ReadDouble(){
	double ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

unsigned short BinaryReader::ReadUInt16(){
	unsigned short ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

unsigned int BinaryReader::ReadUInt32(){
	unsigned int ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

unsigned char BinaryReader::ReadByte(){
	unsigned char ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

string BinaryReader::ReadChars(int count){
	string str;
	for(int i=0;i<count;i++)
		str.push_back(ReadByte());
	this->pos=ftell(this->filePointer);
	return str;
}

long long BinaryReader::ReadInt64(){
	long long ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

unsigned long long BinaryReader::ReadUInt64(){
	unsigned long long ret;
	fread(&ret,sizeof(ret),1,this->filePointer);
	this->pos=ftell(this->filePointer);
	return ret;
}

long BinaryReader::Length(){
	if(this->length!=-1)
		return this->length;
	long sp=ftell(this->filePointer);
	fseek(this->filePointer,0,SEEK_END);
	this->length=ftell(this->filePointer);
	fseek(this->filePointer,sp,SEEK_SET);
	return this->length;
}

long BinaryReader::Position(){
	return ftell(this->filePointer);
}

void BinaryReader::Position(long p){
	fseek(this->filePointer,p,SEEK_SET);
	this->pos=ftell(this->filePointer);
}

void BinaryReader::IncPosition(long c){
	Position(Position()+c);
	this->pos=ftell(this->filePointer);
}

int BinaryReader::_fread(void* ptr,int size,int count){
	int r=fread((void*)ptr,size,count,this->filePointer);
	this->pos=ftell(this->filePointer);
	return r;
}

bool BinaryReader::_EOF(){
	return feof(this->filePointer);
}

std::string BinaryReader::ReadNullString(){
	string ret="";
	char c;
	while((c=BinaryReader::ReadSByte())!=0)
		ret.push_back(c);
	this->pos=ftell(this->filePointer);
	return ret;
}